package com.df.ez_game.tools;

import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class OsHelper {
    private static String osName = null;
    private static String osNameLoserCase = null;
    public static boolean isWindow(){
        if(osName==null){
            getOsName();
        }
        if (osName == null) {
            return false;
        }
        return osName.startsWith("window");
    }

    private static void getOsName() {
        String value = null;
        try {
            value = System.getProperty("os.name");
        } catch (SecurityException e) {
            log.error("读取系统属性失败 '{}';", "os.name");
        }

        if (null == value) {
            try {
                value = System.getenv("os.name");
            } catch (SecurityException e) {
                log.error("读取系统环境变量失败 '{}';", "os.name");
            }
        }
        osName = value;
        if(osName!=null){
            osNameLoserCase = osName.toLowerCase();
        }
    }

    public static boolean isLinux(){
        if(osName==null){
            getOsName();
        }
        if (osName == null) {
            return false;
        }
        return osName.startsWith("linux");
    }
    public static boolean isMac(){
        if(osName==null){
            getOsName();
        }
        if (osName == null) {
            return false;
        }

        return osName.startsWith("mac");
    }

    public static EventLoopGroup getGroupByOs(){
        if(isWindow()){
            return new NioEventLoopGroup();
        }
        if(isLinux()){
            return new EpollEventLoopGroup();
        }
        return new NioEventLoopGroup();
    }
}
